package wangba.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import wangba.dao.ChongzhiDao;
import wangba.dao.ComputerDao;
import wangba.dao.UserDao;
import wangba.dao.WeixiuDao;
import wangba.dao.XiaofeiDao;
import wangba.model.Chongzhi;
import wangba.model.Computer;
import wangba.model.User;
import wangba.model.Weixiu;
import wangba.model.Xiaofei;
import wangba.util.Arith;
import wangba.util.Pager;
import wangba.util.Util;

import com.opensymphony.xwork2.ActionSupport;

public class ManageAction extends ActionSupport {

	private static final long serialVersionUID = -4304509122548259589L;

	private UserDao userDao;

	private String url = "./";

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	
	
	
//登入请求
	public String login() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String  role = request.getParameter("role");
		User user = userDao.selectBean(" where username = '" + username
				+ "' and password= '" + password + "' and role= "+role +" and deletestatus=0 ");
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			this.setUrl("index.jsp");
			return "redirect";
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");
			response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('用户名或者密码错误');window.location.href='login.jsp';</script>");
		}
		return null;
	}
//用户退出
	public String loginout() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		this.setUrl("login.jsp");
		return SUCCESS;
	}
//跳转到修改密码页面
	public String changepwd() {
		this.setUrl("user/user.jsp");
		return SUCCESS;
	}
//修改密码操作
	public void changepwd2() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
		
		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("user");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		User bean = userDao.selectBean(" where username= '"+u.getUsername()+"' and password= '"+password1+"' and deletestatus=0");
		if(bean!=null){
			bean.setPassword(password2);
			userDao.updateBean(bean);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('修改成功');window.location.href='method!changepwd';</script>");
		}else{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('原密码错误');window.location.href='method!changepwd';</script>");
		}
	}
	
	


	//电脑列表
	public String computerlist() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String bianhao = request.getParameter("bianhao");
		String zhuangtai = request.getParameter("zhuangtai");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (zhuangtai != null && !"".equals(zhuangtai)) {

			sb.append("zhuangtai like '%" + zhuangtai + "%'");
			sb.append(" and ");
			request.setAttribute("zhuangtai", zhuangtai);
		}
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		
		
		
		sb.append("  deletestatus=0 order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = computerDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", computerDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!computerlist", "共有" + total + "条记录"));
		request.setAttribute("url", "method!computerlist");
		request.setAttribute("url2", "method!computer");
		request.setAttribute("title", "电脑管理");
		this.setUrl("computer/computerlist.jsp");
		return SUCCESS;

	}
//跳转到添加电脑页面
	public String computeradd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("url", "method!computeradd2");
		request.setAttribute("title", "电脑添加");
		this.setUrl("computer/computeradd.jsp");
		return SUCCESS;
	}
//添加电脑操作
	public void computeradd2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String bianhao = request.getParameter("bianhao");
		String peizhi = request.getParameter("peizhi");
		String shoufei = request.getParameter("shoufei");
		Computer bean = computerDao.selectBean(" where bianhao='"+bianhao+"' and deletestatus=0 ");
		if(bean==null){
			bean = new Computer();
			bean.setBianhao(bianhao);
			bean.setPeizhi(peizhi);
			bean.setShoufei(shoufei);
			bean.setZhuangtai("空闲");
			computerDao.insertBean(bean);
			
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('操作成功');window.location.href='method!computerlist';</script>");
		}else{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('操作失败，该电脑编号已经存在');window.location.href='method!computerlist';</script>");
		}
	}
//跳转到更新电脑页面
	public String computerupdate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Computer bean = computerDao.selectBean(" where id= "
				+ request.getParameter("id"));
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!computerupdate2?id="+bean.getId());
		request.setAttribute("title", "电脑信息修改");
		this.setUrl("computer/computerupdate.jsp");
		return SUCCESS;
	}
//更新电脑操作
	public void computerupdate2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String peizhi = request.getParameter("peizhi");
		String shoufei = request.getParameter("shoufei");
		Computer bean = computerDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setPeizhi(peizhi);
		bean.setShoufei(shoufei);
		computerDao.updateBean(bean);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!computerlist';</script>");
	}
//删除电脑操作
	public void computerdelete() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Computer bean = computerDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setDeletestatus(1);
		computerDao.updateBean(bean);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!computerlist';</script>");
	}
	
	

	
	
	private ComputerDao computerDao;

	public ComputerDao getComputerDao() {
		return computerDao;
	}

	public void setComputerDao(ComputerDao computerDao) {
		this.computerDao = computerDao;
	}

	
	
	//会员列表
	public String userlist() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String truename = request.getParameter("truename");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (username != null && !"".equals(username)) {

			sb.append("username like '%" + username + "%'");
			sb.append(" and ");
			request.setAttribute("username", username);
		}
		
		if (truename != null && !"".equals(truename)) {

			sb.append("truename like '%" + truename + "%'");
			sb.append(" and ");
			request.setAttribute("truename", truename);
		}

		
		
		
		sb.append("   role=2 order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = userDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", userDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!userlist", "共有" + total + "条记录"));
		request.setAttribute("url", "method!userlist");
		request.setAttribute("url2", "method!user");
		request.setAttribute("title", "会员管理");
		this.setUrl("user/userlist.jsp");
		return SUCCESS;

	}
//跳转到添加会员页面
	public String useradd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("url", "method!useradd2");
		request.setAttribute("title", "会员添加");
		this.setUrl("user/useradd.jsp");
		return SUCCESS;
	}
//添加会员操作
	public void useradd2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String jine = request.getParameter("jine");
		String lianxifangshi = request.getParameter("lianxifangshi");
		String truename = request.getParameter("truename");
		User bean = userDao.selectBean(" where username='"+username+"' and deletestatus=0 ");
		if(bean==null){
			bean = new User();
			bean.setCreatetime(new Date());
			bean.setJine(Double.parseDouble(jine));
			bean.setLianxifangshi(lianxifangshi);
			bean.setPassword("111111");
			bean.setRole(2);
			bean.setTruename(truename);
			bean.setUsername(username);
			
			userDao.insertBean(bean);
			
			Chongzhi cz = new Chongzhi();
			cz.setBeizhu("新开会员");
			cz.setCreatetime(Util.getTime());
			cz.setJine(Double.parseDouble(jine));
			cz.setUser(bean);
			chongzhiDao.insertBean(cz);
			
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('操作成功');window.location.href='method!userlist';</script>");
		}else{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('操作失败，该身份证已经存在');window.location.href='method!userlist';</script>");
		}
	}
//跳转到更新会员页面
	public String userupdate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!userupdate2?id="+bean.getId());
		request.setAttribute("title", "会员信息修改");
		this.setUrl("user/userupdate.jsp");
		return SUCCESS;
	}
//更新会员操作
	public void userupdate2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String lianxifangshi = request.getParameter("lianxifangshi");
		String truename = request.getParameter("truename");
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setLianxifangshi(lianxifangshi);
		bean.setTruename(truename);
		userDao.updateBean(bean);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!userlist';</script>");
	}
	//会员停用操作
	public void userdelete() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setDeletestatus(1);
		userDao.updateBean(bean);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!userlist';</script>");
	}
	
	//会员启用操作
	public void userdelete2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setDeletestatus(0);
		userDao.updateBean(bean);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!userlist';</script>");
	}	
	
	
	//跳转到会员充值页面
	public String userupdate3() {
		HttpServletRequest request = ServletActionContext.getRequest();
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!userupdate4?id="+bean.getId());
		request.setAttribute("title", "会员充值");
		this.setUrl("user/userupdate3.jsp");
		return SUCCESS;
	}
//会员充值操作
	public void userupdate4() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String jine = request.getParameter("jine");
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setJine(Arith.add(Double.parseDouble(jine), bean.getJine()));
		userDao.updateBean(bean);
		
		
		Chongzhi cz = new Chongzhi();
		cz.setBeizhu("会员充值");
		cz.setCreatetime(Util.getTime());
		cz.setJine(Double.parseDouble(jine));
		cz.setUser(bean);
		chongzhiDao.insertBean(cz);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!userlist';</script>");
	}
	
	
	private ChongzhiDao chongzhiDao;

	public ChongzhiDao getChongzhiDao() {
		return chongzhiDao;
	}

	public void setChongzhiDao(ChongzhiDao chongzhiDao) {
		this.chongzhiDao = chongzhiDao;
	}
	
	
	//充值记录查询列表
	public String chongzhilist() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String truename = request.getParameter("truename");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (username != null && !"".equals(username)) {

			sb.append("user.username like '%" + username + "%'");
			sb.append(" and ");
			request.setAttribute("username", username);
		}
		
		if (truename != null && !"".equals(truename)) {

			sb.append("user.truename like '%" + truename + "%'");
			sb.append(" and ");
			request.setAttribute("truename", truename);
		}

		
		
		
		sb.append("   1=1 order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = chongzhiDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", chongzhiDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!chongzhilist", "共有" + total + "条记录"));
		request.setAttribute("url", "method!chongzhilist");
		request.setAttribute("url2", "method!chongzhi");
		request.setAttribute("title", "充值记录查询管理");
		this.setUrl("chongzhi/chongzhilist.jsp");
		return SUCCESS;

	}
	
	
	private WeixiuDao weixiuDao;

	public WeixiuDao getWeixiuDao() {
		return weixiuDao;
	}

	public void setWeixiuDao(WeixiuDao weixiuDao) {
		this.weixiuDao = weixiuDao;
	}
	
	
	//维修列表
	public String weixiulist() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String bianhao = request.getParameter("bianhao");
		

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

	
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("computer.bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		
		
		
		sb.append("  1=1 order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = weixiuDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", weixiuDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!weixiulist", "共有" + total + "条记录"));
		request.setAttribute("url", "method!weixiulist");
		request.setAttribute("url2", "method!weixiu");
		request.setAttribute("title", "维修管理");
		this.setUrl("weixiu/weixiulist.jsp");
		return SUCCESS;

	}
//跳转到添加维修页面
	public String weixiuadd() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("computerlist", computerDao.selectBeanList(0, 9999, " where deletestatus=0 and zhuangtai='空闲' "));
		request.setAttribute("url", "method!weixiuadd2");
		request.setAttribute("title", "添加维修记录");
		this.setUrl("weixiu/weixiuadd.jsp");
		return SUCCESS;
	}
//添加维修操作
	public void weixiuadd2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String computer = request.getParameter("computer");
		String guzhang = request.getParameter("guzhang");
		Computer c = computerDao.selectBean(" where id= "+computer);
		Weixiu bean =  new Weixiu();
		bean.setComputer(c);
		bean.setCreatetime(Util.getTime());
		bean.setGuzhang(guzhang);
		bean.setZhuangtai("维修中");
		c.setZhuangtai("维修");
		computerDao.updateBean(c);
		
		weixiuDao.insertBean(bean);
			
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response.getWriter()
					.print(
							"<script language=javascript>alert('操作成功');window.location.href='method!weixiulist';</script>");
		
	}

//完成维修操作
	public void weixiudelete() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Weixiu bean = weixiuDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setZhuangtai("完成维修");
		Computer c = bean.getComputer();
		c.setZhuangtai("空闲");
		computerDao.updateBean(c);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!weixiulist';</script>");
	}
	
	
	//报废操作
	public void weixiudelete2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Weixiu bean = weixiuDao.selectBean(" where id= "
				+ request.getParameter("id"));
		bean.setZhuangtai("报废");
		Computer c = bean.getComputer();
		c.setZhuangtai("报废");
		computerDao.updateBean(c);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
		response
				.getWriter()
				.print(
						"<script language=javascript>alert('操作成功');window.location.href='method!weixiulist';</script>");
	}
	
	
	
	//消费经营
	public String computerlist2() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String bianhao = request.getParameter("bianhao");
		String zhuangtai = request.getParameter("zhuangtai");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (zhuangtai != null && !"".equals(zhuangtai)) {

			sb.append("zhuangtai like '%" + zhuangtai + "%'");
			sb.append(" and ");
			request.setAttribute("zhuangtai", zhuangtai);
		}
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		
		
		
		sb.append("  deletestatus=0 and (zhuangtai='使用' or zhuangtai='空闲' ) order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = computerDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", computerDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!computerlist2", "共有" + total + "条记录"));
		request.setAttribute("url", "method!computerlist2");
		request.setAttribute("url2", "method!computer");
		request.setAttribute("title", "消费经营管理");
		this.setUrl("computer/computerlist2.jsp");
		return SUCCESS;

	}
	
	
	private XiaofeiDao xiaofeiDao;

	public XiaofeiDao getXiaofeiDao() {
		return xiaofeiDao;
	}

	public void setXiaofeiDao(XiaofeiDao xiaofeiDao) {
		this.xiaofeiDao = xiaofeiDao;
	}
	
	
	//跳转到开机信息页面
	public String xiaofeiupdate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Computer bean = computerDao.selectBean(" where id= "+ request.getParameter("id"));
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!xiaofeiupdate2");
		request.setAttribute("title", "开机");
		this.setUrl("xiaofei/xiaofeiupdate.jsp");
		return SUCCESS;
	}
//更新开机信息操作
	public void xiaofeiupdate2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String username = request.getParameter("username");
		Computer com = computerDao.selectBean(" where id= "
				+ request.getParameter("computerid"));
		User user = userDao.selectBean(" where username='"+username+"' and deletestatus=0 ");
		if(user==null){
			
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('操作失败，该身份证不存在');window.location.href='method!xiaofeiupdate?id="+com.getId()+"';</script>");
			return;
		}
		if("使用".equals(com.getZhuangtai())){
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('操作失败，该电脑正在使用');window.location.href='method!xiaofeiupdate?id="+com.getId()+"';</script>");
			return;
		}
		
		Xiaofei xf = xiaofeiDao.selectBean(" where  user.id="+user.getId() +" and  zhuangtai='未结账'");
		if(xf!=null){
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('操作失败，该身份证正在使用');window.location.href='method!xiaofeiupdate?id="+com.getId()+"';</script>");
			return;
		}
		
		
		com.setZhuangtai("使用");
		computerDao.updateBean(com);
		
		String beizhu = request.getParameter("beizhu");
		String time1 = request.getParameter("time1");
		Xiaofei bean = new Xiaofei();
		bean.setBeizhu(beizhu);
		bean.setComputer(com);
		bean.setTime1(time1);
		bean.setUser(user);
		bean.setZhuangtai("未结账");
		
		xiaofeiDao.insertBean(bean);
		
		response
		.getWriter()
		.print(
				"<script language=javascript>alert('操作成功');window.location.href='method!xiaofeilist';</script>");
		
	}
	
	
	//结账列表
	public String xiaofeilist() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		List<Xiaofei> list = xiaofeiDao.selectBeanList(0,9999," where zhuangtai='未结账' ");
		for(Xiaofei xf:list){
			xf.setTime2(Util.getTime());
			long t1 = Util.getTime3(xf.getTime2()).getTime();//当前时间毫秒数
			
			long t2 = Util.getTime3(xf.getTime1()).getTime();//上机时间毫秒数
			
			long t3 = 60*1000;//一分钟的毫秒数
			
			long shijian = ((t1-t2)/t3 );//分钟数
			
			xf.setShijian((int)shijian);
			
			double d1 = Double.parseDouble(xf.getComputer().getShoufei().replaceAll("元", ""));
			
			double d2 = Arith.mul(d1, shijian);
			double d3 = Arith.div(d2, 60, 1);
			xf.setXiaofei(d3);
			
			
			xiaofeiDao.updateBean(xf);
			
		}
		
		
		String bianhao = request.getParameter("bianhao");
		String username = request.getParameter("username");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (username != null && !"".equals(username)) {

			sb.append("user.username like '%" + username + "%'");
			sb.append(" and ");
			request.setAttribute("username", username);
		}
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("computer.bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		
		
		
		sb.append("  zhuangtai='未结账' order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = xiaofeiDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", xiaofeiDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!xiaofeilist", "共有" + total + "条记录"));
		request.setAttribute("url", "method!xiaofeilist");
		request.setAttribute("url2", "method!xiaofei");
		request.setAttribute("title", "结账管理");
		this.setUrl("xiaofei/xiaofeilist.jsp");
		return SUCCESS;

	}
	
	
	//跳转到结账信息页面
	public String xiaofeiupdate3() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Xiaofei bean = xiaofeiDao.selectBean(" where id= "+ request.getParameter("id"));
		
		
		bean.setTime2(Util.getTime());
		long t1 = Util.getTime3(bean.getTime2()).getTime();//当前时间毫秒数
		
		long t2 = Util.getTime3(bean.getTime1()).getTime();//上机时间毫秒数
		
		long t3 = 60*1000;//一分钟的毫秒数
		
		long shijian = ((t1-t2)/t3 );//分钟数
		
		bean.setShijian((int)shijian);
		
		double d1 = Double.parseDouble(bean.getComputer().getShoufei().replaceAll("元", ""));
		
		double d2 = Arith.mul(d1, shijian);
		double d3 = Arith.div(d2, 60, 1);
		bean.setXiaofei(d3);
		
		
		xiaofeiDao.updateBean(bean);
		
		
		
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!xiaofeiupdate4?id="+bean.getId());
		request.setAttribute("title", "结账");
		this.setUrl("xiaofei/xiaofeiupdate3.jsp");
		return SUCCESS;
	}
//结账操作
	public void xiaofeiupdate4() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");

		Xiaofei bean = xiaofeiDao.selectBean(" where id= "+ request.getParameter("id"));
		bean.setZhuangtai("已结账");
		xiaofeiDao.updateBean(bean);
		
		Computer c = bean.getComputer();
		c.setZhuangtai("空闲");
		computerDao.updateBean(c);
		
		User u = bean.getUser();
		u.setXiaofei(u.getXiaofei()+bean.getXiaofei());
		
		userDao.updateBean(u);
		
		
		response
		.getWriter()
		.print(
				"<script language=javascript>alert('操作成功');window.location.href='method!xiaofeilist2';</script>");
		
	}
	
	
	
	//消费记录列表
	public String xiaofeilist2() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		
		
		
		String bianhao = request.getParameter("bianhao");
		String username = request.getParameter("username");

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		if (username != null && !"".equals(username)) {

			sb.append("user.username like '%" + username + "%'");
			sb.append(" and ");
			request.setAttribute("username", username);
		}
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("computer.bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		
		
		
		sb.append("  zhuangtai='已结账' order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = xiaofeiDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", xiaofeiDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!xiaofeilist2", "共有" + total + "条记录"));
		request.setAttribute("url", "method!xiaofeilist2");
		request.setAttribute("url2", "method!xiaofei");
		request.setAttribute("title", "消费记录列表");
		this.setUrl("xiaofei/xiaofeilist2.jsp");
		return SUCCESS;

	}
	
	
	//会员信息查询
	public String userlist2() {
		HttpServletRequest request = ServletActionContext.getRequest();


		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		

		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		
		
		sb.append("   id="+user.getId()+" order by id desc ");
		String where = sb.toString();


		request.setAttribute("list", userDao.selectBeanList(0, 1, where));

		request.setAttribute("title", "会员信息查询");
		this.setUrl("user/userlist2.jsp");
		return SUCCESS;

	}
	
	
	//充值记录查询列表
	public String chongzhilist2() {
		HttpServletRequest request = ServletActionContext.getRequest();
		

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		
		sb.append("   user.id="+user.getId()+" order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = chongzhiDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", chongzhiDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!chongzhilist2", "共有" + total + "条记录"));
		request.setAttribute("url", "method!chongzhilist2");
		request.setAttribute("url2", "method!chongzhi");
		request.setAttribute("title", "充值记录查询管理");
		this.setUrl("chongzhi/chongzhilist2.jsp");
		return SUCCESS;

	}
	
	
	
	//自助结账
	public String xiaofeilist3() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		List<Xiaofei> list = xiaofeiDao.selectBeanList(0,9999," where zhuangtai='未结账' ");
		for(Xiaofei xf:list){
			xf.setTime2(Util.getTime());
			long t1 = Util.getTime3(xf.getTime2()).getTime();//当前时间毫秒数
			
			long t2 = Util.getTime3(xf.getTime1()).getTime();//上机时间毫秒数
			
			long t3 = 60*1000;//一分钟的毫秒数
			
			long shijian = ((t1-t2)/t3 );//分钟数
			
			xf.setShijian((int)shijian);
			
			double d1 = Double.parseDouble(xf.getComputer().getShoufei().replaceAll("元", ""));
			
			double d2 = Arith.mul(d1, shijian);
			double d3 = Arith.div(d2, 60, 1);
			xf.setXiaofei(d3);
			
			
			xiaofeiDao.updateBean(xf);
			
		}
		
	

		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		

		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		
		sb.append("  zhuangtai='未结账' and user.id="+user.getId()+" order by id desc ");
		String where = sb.toString();



		request.setAttribute("list", xiaofeiDao.selectBeanList(0, 1, where));

		request.setAttribute("url2", "method!xiaofei");
		request.setAttribute("title", "自助结账");
		this.setUrl("xiaofei/xiaofeilist3.jsp");
		return SUCCESS;

	}
	
	
	//跳转到结账信息页面
	public String xiaofeiupdate5() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Xiaofei bean = xiaofeiDao.selectBean(" where id= "+ request.getParameter("id"));
		
		
		bean.setTime2(Util.getTime());
		long t1 = Util.getTime3(bean.getTime2()).getTime();//当前时间毫秒数
		
		long t2 = Util.getTime3(bean.getTime1()).getTime();//上机时间毫秒数
		
		long t3 = 60*1000;//一分钟的毫秒数
		
		long shijian = ((t1-t2)/t3 );//分钟数
		
		bean.setShijian((int)shijian);
		
		double d1 = Double.parseDouble(bean.getComputer().getShoufei().replaceAll("元", ""));
		
		double d2 = Arith.mul(d1, shijian);
		double d3 = Arith.div(d2, 60, 1);
		bean.setXiaofei(d3);
		
		
		xiaofeiDao.updateBean(bean);
		
		
		
		request.setAttribute("bean", bean);
		request.setAttribute("url", "method!xiaofeiupdate6?id="+bean.getId());
		request.setAttribute("title", "结账");
		this.setUrl("xiaofei/xiaofeiupdate3.jsp");
		return SUCCESS;
	}
//结账操作
	public void xiaofeiupdate6() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");

		Xiaofei bean = xiaofeiDao.selectBean(" where id= "+ request.getParameter("id"));
		bean.setZhuangtai("已结账");
		xiaofeiDao.updateBean(bean);
		
		Computer c = bean.getComputer();
		c.setZhuangtai("空闲");
		computerDao.updateBean(c);
		
		User u = bean.getUser();
		u.setXiaofei(u.getXiaofei()+bean.getXiaofei());
		
		userDao.updateBean(u);
		
		
		response
		.getWriter()
		.print(
				"<script language=javascript>alert('操作成功');window.location.href='method!xiaofeilist4';</script>");
		
	}
	
	
	//消费记录列表
	public String xiaofeilist4() throws ParseException {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		
		
		
		String bianhao = request.getParameter("bianhao");


		
		StringBuffer sb = new StringBuffer();
		sb.append(" where ");

		
		
		if (bianhao != null && !"".equals(bianhao)) {

			sb.append("computer.bianhao like '%" + bianhao + "%'");
			sb.append(" and ");
			request.setAttribute("bianhao", bianhao);
		}

		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		
		sb.append("  zhuangtai='已结账' and user.id="+user.getId()+" order by id desc ");
		String where = sb.toString();


		int currentpage = 1;
		int pagesize = 10;
		if (request.getParameter("pagenum") != null) {
			currentpage = Integer.parseInt(request.getParameter("pagenum"));
		}
		int total = xiaofeiDao.selectBeanCount(where.replaceAll(" order by id desc ", ""));
		request.setAttribute("list", xiaofeiDao.selectBeanList((currentpage - 1)
				* pagesize, pagesize, where));
		request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
				currentpage, "method!xiaofeilist4", "共有" + total + "条记录"));
		request.setAttribute("url", "method!xiaofeilist4");
		request.setAttribute("url2", "method!xiaofei");
		request.setAttribute("title", "消费记录列表");
		this.setUrl("xiaofei/xiaofeilist4.jsp");
		return SUCCESS;

	}
	
}
